package com.aidex.biz.controller;

import java.util.List;

import com.aidex.biz.domain.CustomerDongtai;
import com.aidex.biz.domain.CustomerShadowInfo;
import com.aidex.biz.service.CustomerDongtaiService;
import com.aidex.biz.service.CustomerShadowInfoService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.ToHome;
import com.aidex.biz.service.ToHomeService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 预约上门Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-11-23
 */
@RestController
@RequestMapping("/biz/bizToHome")
public class ToHomeController extends BaseController
{
    @Autowired
    private ToHomeService toHomeService;
    @Autowired
    private CustomerDongtaiService customerDongtaiService;
    @Autowired
    private CustomerShadowInfoService customerShadowInfoService;

    /**
     * 查询预约上门列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(ToHome toHome, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                toHome.setCompanyId(null);
        }else{
                     toHome.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        toHome.setPage(new PageDomain(request, response));
        return R.data(toHomeService.findPage(toHome));
    }

    /**
     * 获取预约上门详细信息
     */
    @GetMapping(value = "/{id}")
    public R<ToHome> detail(@PathVariable("id") String id)
    {
        return R.data(toHomeService.get(id));
    }

    /**
     * 新增预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  ToHome toHome)
    {
        return R.status(toHomeService.save(toHome));
    }

    /**
     * 修改预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ToHome toHome)
    {
        if(toHome.getIsFanKui()==1){
            CustomerDongtai customerDongtai=new CustomerDongtai();
            customerDongtai.setCustomerId(toHome.getCustomerId());
            customerDongtai.setType("3");
            customerDongtai.setContent(SecurityUtils.getLoginUser().getUser().getName()+"-添加上门反馈");
            customerDongtaiService.save(customerDongtai);
            // 添加跟进记录
            CustomerShadowInfo customerShadowInfo=new CustomerShadowInfo();
            customerShadowInfo.setTakes(toHome.getRemark());
            customerShadowInfoService.save(customerShadowInfo);

        }
        return R.status(toHomeService.save(toHome));
    }


    /**
     * 删除预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(toHomeService.deleteToHomeByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(toHomeService.findMaxSort(new ToHome()));
    }

    /**
     * 导出预约上门列表
     */
    @Log(title = "预约上门", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ToHome toHome)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    toHome.setCompanyId(null);
            }else{
                         toHome.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<ToHome> list = toHomeService.findList(toHome);
        ExcelUtil<ToHome> util = new ExcelUtil<ToHome>(ToHome.class);
        return util.exportExcel(list, "预约上门数据");
    }

}
